/*
 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2008 Diego Hidalgo C. Gonzalez
 * Copyright (C) 2009-2010 ProFUSION embedded systems
 * Copyright (C) 2009-2010 Samsung Electronics
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "PlatformKeyboardEvent.h"

#include "CdKeyboardUtilities.h"
#include "NotImplemented.h"
#include "core/keycodes.h"
#include "WindowsKeyboardCodes.h"
#include "TextEncoding.h"
#include <wtf/CurrentTime.h>
#include "view/view.h"
namespace WebCore {

#if PLATFORM(CDROID)
static PlatformEvent::Type eventTypeForKeyEvent(const cdroid::KeyEvent& event){
    return event.getAction() == cdroid::KeyEvent::ACTION_UP ? PlatformEvent::KeyUp : PlatformEvent::KeyDown;
}

static PlatformEvent::Modifiers modifiersForKeyEvent(const cdroid::KeyEvent& event){
    unsigned int modifiers = 0;
    if (event.isShiftPressed())
        modifiers |= PlatformEvent::ShiftKey;
    if (event.isCtrlPressed())
        modifiers |= PlatformEvent::CtrlKey;
    if (event.isAltPressed())
        modifiers |= PlatformEvent::AltKey;
    if (event.isMetaPressed())
        modifiers |= PlatformEvent::MetaKey;
    return static_cast<PlatformEvent::Modifiers>(modifiers);
}
static int windowsKeyCodeForKeyEvent(unsigned int keyCode)
{
    // Does not provide all key codes, and does not handle all keys.
    switch (keyCode) {
    case KEY_DEL:        return VK_BACK;
    case KEY_TAB:        return VK_TAB;
    case KEY_CLEAR:      return VK_CLEAR;
    case KEY_DPAD_CENTER:
    case KEY_ENTER:
        return VK_RETURN;
    case KEY_SHIFT_LEFT:
    case KEY_SHIFT_RIGHT:
        return VK_SHIFT;
    // back will serve as escape, although we probably do not have access to it
    case KEY_BACK:        return VK_ESCAPE;
    case KEY_SPACE:       return VK_SPACE;
    case KEY_HOME:        return VK_HOME;
    case KEY_DPAD_LEFT:   return VK_LEFT;
    case KEY_DPAD_UP:     return VK_UP;
    case KEY_DPAD_RIGHT:  return VK_RIGHT;
    case KEY_DPAD_DOWN:   return VK_DOWN;
    case KEY_0:        return VK_0;
    case KEY_1:        return VK_1;
    case KEY_2:        return VK_2;
    case KEY_3:        return VK_3;
    case KEY_4:        return VK_4;
    case KEY_5:        return VK_5;
    case KEY_6:        return VK_6;
    case KEY_7:        return VK_7;
    case KEY_8:        return VK_8;
    case KEY_9:        return VK_9;
    case KEY_A:        return VK_A;
    case KEY_B:        return VK_B;
    case KEY_C:        return VK_C;
    case KEY_D:        return VK_D;
    case KEY_E:        return VK_E;
    case KEY_F:        return VK_F;
    case KEY_G:        return VK_G;
    case KEY_H:        return VK_H;
    case KEY_I:        return VK_I;
    case KEY_J:        return VK_J;
    case KEY_K:        return VK_K;
    case KEY_L:        return VK_L;
    case KEY_M:        return VK_M;
    case KEY_N:        return VK_N;
    case KEY_O:        return VK_O;
    case KEY_P:        return VK_P;
    case KEY_Q:        return VK_Q;
    case KEY_R:        return VK_R;
    case KEY_S:        return VK_S;
    case KEY_T:        return VK_T;
    case KEY_U:        return VK_U;
    case KEY_V:        return VK_V;
    case KEY_W:        return VK_W;
    case KEY_X:        return VK_X;
    case KEY_Y:        return VK_Y;
    case KEY_Z:        return VK_Z;
    // colon
    case KEY_SEMICOLON:    return VK_OEM_1;
    case KEY_COMMA:        return VK_OEM_COMMA;
    case KEY_MINUS:        return VK_OEM_MINUS;
    case KEY_EQUALS:       return VK_OEM_PLUS;
    case KEY_PERIOD:       return VK_OEM_PERIOD;
    case KEY_SLASH:        return VK_OEM_2;
    // maybe not the right choice
    //case KEY_LEFT_BRACKET: return VK_OEM_4;
    case KEY_BACKSLASH:    return VK_OEM_5;
    //case KEY_RIGHT_BRACKET:return VK_OEM_6;
    default:
        return 0;
    }
}

static String keyIdentifierForAndroidKeyCode(int keyCode)
{
    // Does not return all of the same key identifiers, and
    // does not handle all the keys.
    switch (keyCode) {
    case KEY_CLEAR:
        return "Clear";
    case KEY_ENTER:
    case KEY_DPAD_CENTER:
        return "Enter";
    case KEY_HOME:
        return "Home";
    case KEY_DPAD_DOWN:
        return "Down";
    case KEY_DPAD_LEFT:
        return "Left";
    case KEY_DPAD_RIGHT:
        return "Right";
    case KEY_DPAD_UP:
        return "Up";
    // Standard says that DEL becomes U+00007F.
    case KEY_DEL:
        return "U+00007F";
    default:
        char upper[16];
        sprintf(upper, "U+%04X", windowsKeyCodeForKeyEvent(keyCode));
        return String(upper);
    }
}
PlatformKeyboardEvent::PlatformKeyboardEvent(const cdroid::KeyEvent&event)
    : PlatformEvent(eventTypeForKeyEvent(event),modifiersForKeyEvent(event),currentTime())
	/*
    , m_text(String::fromUTF8(event->string))
    , m_unmodifiedText(String::fromUTF8(event->string))
    , m_keyIdentifier(keyIdentifierForEvasKeyName(String(event->key)))
    , m_windowsVirtualKeyCode(windowsKeyCodeForEvasKeyName(String(event->key)))
    , m_nativeVirtualKeyCode(0)
    , m_macCharCode(0)
    , m_autoRepeat(false)
    , m_isKeypad(String(event->key).startsWith("KP_"))
    , m_isSystemKey(false)*/
{
    m_keyIdentifier = keyIdentifierForAndroidKeyCode(event.getKeyCode());
    m_windowsVirtualKeyCode = windowsKeyCodeForKeyEvent(event.getKeyCode()); 
}
#endif

void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool)
{
    ASSERT(m_type == KeyDown);
    m_type = type;

    if (type == PlatformEvent::RawKeyDown) {
        m_text = String();
        m_unmodifiedText = String();
    } else {
        m_keyIdentifier = String();
        m_windowsVirtualKeyCode = 0;
    }
}

bool PlatformKeyboardEvent::currentCapsLockState()
{
    notImplemented();
    return false;
}

void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
{
    notImplemented();
    shiftKey= false;//m_modifiers & PlatformEvent::ShiftKey;
    ctrlKey = false;//m_modifiers & PlatformEvent::CtrlKey;
    altKey  = false;//m_modifiers & PlatformEvent::AltKey;
    metaKey = false;//m_modifiers & PlatformEvent::MetaKey;
}

}
